package com.c.ctgproject.ui.storage

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.c.ctgproject.base.BaseViewModel
import com.c.ctgproject.mode.StorageVehicles
import com.c.ctgproject.retrofit.HttpHelper
import com.c.ctgproject.retrofit.Response
import com.c.ctgproject.retrofit.Service
import com.c.ctgproject.utils.file2Base64
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.io.File

class StorageHomeViewModel : BaseViewModel() {
    private val service = HttpHelper.getInstance().create(Service::class.java)
    private val mResponseData: MutableLiveData<Response<*>> = MutableLiveData()
    val responseData: LiveData<Response<*>> get() = mResponseData

    private val mStorageVehiclesData: MutableLiveData<Response<StorageVehicles>> = MutableLiveData()
    val storageVehiclesData: LiveData<Response<StorageVehicles>> get() = mStorageVehiclesData

    //识别车牌
    fun licensePlate(token: String, file: File) {
        Log.d("日志", "上传开始照片")
        val base64 = file2Base64(file)
        service.licensePlate(token, base64)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Response<*>> {
                override fun onError(e: Throwable) {
                    Log.d("错误信息", "" + e.message)
                    mError.value = e.message
                    e.printStackTrace()
                }

                override fun onComplete() {
                    Log.d("日志", "onComplete")
                }

                override fun onSubscribe(d: Disposable) {
                    Log.d("日志", "onSubscribe")
                }

                override fun onNext(response: Response<*>) {
                    Log.d("日志", "onNext")
                    Log.d("日志response", response.toString())
                    mResponseData.value = response
                    Log.d("日志", "onNext结束")
                }
            })
    }

    //获取仓储车辆
    fun primaryInspection(token: String, carPlate: String) {
        service.primaryInspection(token, carPlate)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Response<StorageVehicles>> {
                override fun onError(e: Throwable) {
                    Log.d("错误信息", "" + e.message)
                    e.printStackTrace()
                }

                override fun onComplete() {}
                override fun onSubscribe(d: Disposable) {}
                override fun onNext(response: Response<StorageVehicles>) {
                    mStorageVehiclesData.value = response
                }
            })
    }
}